package com.czl.module_storehouse.activity.remand.damage;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;

import com.czl.base.data.bean.tengyun.RecordRemandBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.activity.BaseActivity;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.utils.ParcelHelper;
import com.czl.module_storehouse.adapter.DirectDamageSearchAdapter;
import com.czl.module_storehouse.bean.RemandReturnBean;
import com.czl.module_storehouse.databinding.ActivityDirectDamageBinding;
import com.czl.module_storehouse.event.DamageRegisPostEvent;
import com.czl.module_storehouse.event.DamageRegisSearchEvent;
import com.czl.module_storehouse.event.DirectDamageEvent;
import com.czl.module_storehouse.mvp.presenter.RemandHomePresenter;
import com.czl.module_storehouse.mvp.view.RemandHomeView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xubin
 */
public class DirectDamageActivity extends BaseActivity<ActivityDirectDamageBinding> implements RemandHomeView {

    @InjectPresenter
    RemandHomePresenter mRemandHomePresenter;
    private DirectDamageSearchAdapter mAdapter;
    private final RemandReturnBean mRemandReturnBean = new RemandReturnBean();

    @Override
    protected ActivityDirectDamageBinding getBinding(LayoutInflater layoutInflater) {
        return ActivityDirectDamageBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mToolBinding.toolbarContentTitle.setText("损坏登记");
        mAdapter = new DirectDamageSearchAdapter(new ArrayList<>());
        binding.recyclerView.setAdapter(mAdapter);
        EventBus.getDefault().register(this);
        initListener();
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(DamageRegisPostEvent event) {
        mAdapter.add(event.getSortBean());
    }

    protected void initListener() {
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            RecordRemandBean item = mAdapter.getItem(position);
            if (item != null && item.getItemType() == RecordRemandBean.VIEW_TYPE_CHILDREN) {
                nextDamageActivity(item.getSortBean());
            }
        });
        binding.llSearch.setOnClickListener(v -> nextSearch());
        binding.textConfirm.setOnClickListener(v -> submit());
    }

    private void nextDamageActivity(SortBean bean) {
        EventBus.getDefault().postSticky(new DamageRegisSearchEvent(bean));
        getContext().startActivity(new Intent(getContext(), DamageRegisProductActivity.class));
    }

    private void nextSearch() {
        startActivity(new Intent(getContext(), DirectDamageSearchActivity.class));
    }

    private void submit() {
        List<RecordRemandBean> data = mAdapter.getData();
        List<RecordRemandBean> newList = new ArrayList<>();
        List<SortBean> childs = new ArrayList<>();
        RecordRemandBean recordRemandBean = null;
        for (RecordRemandBean bean : data) {
            int itemType = bean.getItemType();
            if (itemType == RecordRemandBean.VIEW_TYPE_PARENT) {
                recordRemandBean = bean;
                newList.add(bean);
                childs = new ArrayList<>();
            } else {
                SortBean beanSortBean = bean.getSortBean();
                SortBean copy = ParcelHelper.copy(beanSortBean);
                copy.setParentRecordBean(null);
                childs.add(copy);
                if (recordRemandBean != null) {
                    recordRemandBean.setSortList(childs);
                }
            }
        }
        mRemandReturnBean.setRecordList(newList);
        mRemandHomePresenter.directReturn(mRemandReturnBean, 0);
    }

    @Override
    public <T> void showHttpResponse(HttpResponse<T> httpResponse) {
        super.showHttpResponse(httpResponse);
        if (RemandHomePresenter.TAG_REMAND_DIRECT_RETURN.equals(httpResponse.getRequestTag())) {
            EventBus.getDefault().postSticky(new DirectDamageEvent());
            showToast("归还成功");
            finish();
        }
    }
}
